#include <fcntl.h>
#include <utmp.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define UTMP	"/var/run/utmp"

int main (int argc, char *argv[])
{
	
	struct utmp ut ;
	int size, fin, fout ;
	int wspell = 0 ;
        char type[255];
        time_t date, *pdate;
        unsigned char bytes[4];
        
        pdate = &date;
	size = sizeof(ut) ;

	fin = open (UTMP, O_RDONLY) ;
	if (fin < 0) 
        {
		fprintf(stderr, "[error] %s\n", strerror(errno)) ;
		exit (0) ;
	}
	else 
        {
            printf( "Information about who's using the system.\n" );
    
	    while( read( fin, &ut, size ) == size ) 
            {  
                date = ut.ut_time;
                
                switch(ut.ut_type) 
                {
                    case EMPTY:
                        strcpy( type, "Unused space in file." );
                        break;
                    case RUN_LVL:
                        strcpy( type, "The run level of the process, as defined in the inittab file." );
                        break;
                        
                    case BOOT_TIME:
                        strcpy( type, "The time at which the system was started." );
                        break;
                        
                    case OLD_TIME:
                        strcpy( type, "The time at which a user logged on to the system." );
                        break;
                        
                    case NEW_TIME:
                        strcpy( type, "The amount of time the user is idle." );
                        break;
                        
                    case INIT_PROCESS:
                        strcpy( type, "A process spawned by the init command." );
                        break;
                        
                    case LOGIN_PROCESS:
                        strcpy( type, "A getty process waiting for a login." );
                        break;
                        
                    case USER_PROCESS:
                        strcpy( type, "A user process." );
                        break;
                        
                    case DEAD_PROCESS:
                        strcpy( type, "A zombie process." );
                        break;
                        
                    case ACCOUNTING:
                        strcpy( type, "A system accounting process." );
                        break;
                }

		fprintf( stdout, "Login type:%8s%s\n", "", type );
		fprintf( stdout, "Process PID:%7s%d\n", "", ut.ut_pid );
		fprintf( stdout, "TTY name:%10s%s\n", "", ut.ut_line );
		fprintf( stdout, "Abbr.TTY:%10s%s\n", "", ut.ut_id );
		fprintf( stdout, "Login Time:%8s%s", "", ctime( pdate ) );
		fprintf( stdout, "Username:%10s%s\n", "", ut.ut_user );
		fprintf( stdout, "Hostname:%10s%s\n", "", ut.ut_host );
		fprintf( stdout, "IP Address:%8s%d.%d.%d.%d\n", "", ut.ut_addr_v6[0], ut.ut_addr_v6[1], ut.ut_addr_v6[2], ut.ut_addr_v6[3]  );
		fprintf( stdout, "\n\n" );
            }
        }
		
	close(fin);
	exit (0);
} 
